<?php
/**
 * @ClassName: OperationCenter
 * @Description: 运营中心
 * @author: gydtrade
 * @Date: 2025-05-28 13:39
 * @Version: V1.0.0
 */

namespace app\common\model;

use think\Model;
use tpext\areacity\api\model\Areacity;

class OperationCenter extends Model
{
    protected $name = 'operation_center';

    protected $autoWriteTimestamp = 'dateTime';

    protected $append = ['parent_name', 'pcat', 'total_amount', 'balance'];

    public static $center_types = [
        3 => '省级运营中心',
        2 => '市级运营中心',
        1 => '区县级运营中心',
    ];

    public static $opening_types = [
        1 => '平台开通',
        2 => '上级开通',
    ];

    public function getParentNameAttr($value, $data)
    {
        return self::where('id', $data['parent_id'])->value('name');
    }

    public function getPcatAttr($value, $data)
    {
        $text = '';
        $province = Areacity::where(['id' => $data['province']])->find();

        if ($province) {
            $text .= $province['ext_name'];
            $city = Areacity::where(['id' => $data['city']])->find();

            if ($city) {
                $text .= ',' . $city['ext_name'];
                $area = Areacity::where(['id' => $data['area']])->find();

                if ($area) {
                    $text .= ',' . $area['ext_name'];
                }
            }
        }

        return $text;
    }

    public function getTotalAmountAttr($value, $data)
    {
        return 0;
    }

    public function getBalanceAttr($value, $data)
    {
        return 0;
    }

    public function member()
    {
        return $this->belongsTo(Member::class, 'member_id', 'id');
    }

    public function agent()
    {
        return $this->belongsTo('agent', 'agent_id', 'id');
    }

}